from django.db import models
from django.utils.timezone import now
from django.contrib.auth import get_user_model
from ckeditor.fields import RichTextField
# Create your models here.

User = get_user_model()

#在数据库创建新闻标签
class NewsTag(models.Model):
    name = models.CharField(max_length=20, verbose_name='新闻标签')

    class Meta:
        verbose_name = '新闻标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        '''将新闻标签对象改为其本身名'''
        return self.name

#创建新闻必要的信息，便于获取和添加
class NewsDetail(models.Model):
    title = models.CharField(max_length=50, verbose_name='新闻标题')
    content = RichTextField(verbose_name='新闻内容')
    image = models.ImageField(upload_to='makenews_detail', verbose_name='新闻图片')
    source = models.TextField(verbose_name='新闻来源')
    add_time = models.DateTimeField(auto_now=True)
    tag = models.ManyToManyField(NewsTag, blank=True)

    class Meta:

        verbose_name = '新闻详细信息'
        verbose_name_plural = verbose_name

    def __str__(self):
        '''将新闻属性对象名改为自定义的名字'''
        return self.title

#创建新闻评论包含的属性，并与新闻属性关联
class NewsComments(models.Model):
    news = models.ForeignKey(NewsDetail, verbose_name='评论的新闻')
    comments = models.TextField(max_length=2000, verbose_name='新闻的评论')
    user = models.ForeignKey(User, verbose_name='评论的用户')
    add_time = models.DateTimeField(auto_now=True, verbose_name='评论时间')
